/**
 * Created by Tomkk on 2016/5/13.
 */
angular.module('app.controller', [])
    //首页
    .controller('HomeCtrl', function ($scope, $state, LoginService, RegisterService, SystemService) {

        $scope.loginUser = {
            username: '',
            password: ''
        };
        $scope.registerUser = {
            username: "",
            password: "",
            nickname: "",
            realname: "",
            mobile: "",
            email: ""
        };

        $scope.checkUsernameIsUsed = function () {
            if ($scope.registerUser.username.length < 6) {
                $scope.usernameError = 2;//用户名长度小于6位
                return;
            }
            var promise = RegisterService.checkUsernameIsUsed($scope.registerUser.username);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.isUsernameUsed = 0;//用户名可用
                } else {
                    $scope.isUsernameUsed = 1;//用户名已被注册
                }
            })
        }
        $scope.checkUsernameLength = function () {
            if ($scope.registerUser.username.length < 6) {
                $scope.usernameError = 1;//用户名长度小于6位
            } else {
                $scope.usernameError = 0;
            }
        }

        $scope.checkPassLength = function () {
            if ($scope.registerUser.password.length < 6)
                $scope.passwordError = 1;
            else {
                $scope.passwordError = 0;
            }
        }

        $scope.checkPass = function () {
            if ($scope.repeatPassword != $scope.registerUser.password) {
                $scope.passNotSameError = true;

            } else {
                $scope.passNotSameError = false;
            }
        };

        $scope.checkPhoneLength = function () {
            if (!/^\d{11}$/.test($scope.registerUser.mobile))
                $scope.mobileError = 1;
            else {
                $scope.mobileError = 0;
            }
        }
        $scope.checkEmail = function () {
            if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test($scope.registerUser.email)) {
                $scope.emailError = 1;
            } else {
                $scope.emailError = 0;
            }
        }

        $scope.repeatPassword = "";
        $scope.passNotSameError = false;
        $scope.isLogin = false;
        $('#loginModal').on('hidden.bs.modal', function () {
            if ($scope.isLogin) {
                $state.go('startClub');
            }
        })

        $scope.visitor = function () {
            SystemService.setCurrentUser(
                {
                    role: 0,
                    email: "",
                    headimage: "",
                    mobile: "",
                    nickname: "",
                    realname: "",
                    user_id: -1,
                    user_isuse: "",
                    username: ""
                }
            )
            swal("亲爱的游客", "欢迎您的到来!", "success", "OK");
            window.sessionStorage.isLogin = true;
            $state.go('startClub');
        }

        $scope.login = function () {
            if (!$scope.loginUser.username || !$scope.loginUser.password) {
                swal("失败", "请完全填写表单!", "warning", "OK");
                return;
            }
            var promise = LoginService.login($scope.loginUser);
            promise.then(function (data) {
                if (data.code == 0) {
                    //console.log(data);
                    $scope.loginUser = {};
                    if (typeof data.returnValue == "string") {
                        $('#loginModal').modal('hide');
                        $scope.isLogin = true;
                        SystemService.setCurrentUser(
                            {
                                role: 0,
                                email: "",
                                headimage: "",
                                mobile: "",
                                nickname: "",
                                realname: "",
                                user_id: "",
                                user_isuse: "",
                                username: ""
                            }
                        )
                        swal("亲爱的游客", "欢迎您的到来!", "success", "OK");
                        window.sessionStorage.isLogin = true;
                        return;
                    }
                    SystemService.setCurrentUser(data.returnValue);
                    $('#loginModal').modal('hide');
                    $scope.isLogin = true;
                    window.sessionStorage.isLogin = true;
                    swal("亲爱的  " + data.returnValue.nickname, "欢迎回来!", "success", "OK");
                }
                else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };

        $scope.isClick = false;
        $scope.register = function () {
            if (!$scope.isClick) {
                $scope.isClick = true;
                $scope.checkUsernameIsUsed();
                $scope.checkUsernameLength();
                $scope.checkPassLength();
                $scope.checkPass();
                $scope.checkPhoneLength();
                $scope.checkEmail();
                if ($scope.usernameError||$scope.isUsernameUsed||$scope.passNotSameError || $scope.usernameError || $scope.passwordError || $scope.mobileError || $scope.emailError) {
                    return;
                }
                for (var key in $scope.registerUser) {
                    if (!$scope.registerUser[key]) {
                        swal("失败", "请完全填写表单!", "warning", "OK");
                        return;
                    }
                }
                var promise = RegisterService.register($scope.registerUser);
                promise.then(function (data) {
                    var role = '';
                    if (data.code == 0) {
                        $('#registerModal').modal('hide');
                        swal("成功", "等待管理员审核身份,审核前登陆将默认为游客身份", "success", "OK");
                    } else {
                        swal("失败", data.errorReason, "warning", "OK");
                    }
                    $scope.isClick = false;
                })
            }
        }
    })
    //登陆后的首页
    .controller('StartClubCtrl', function ($scope, $state, StartClubService, ShowNewsService, SystemService, ManageUsersService, ShowAchievementService) {
        if (!window.sessionStorage.isLogin) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        }
        $scope.isShow = (SystemService.getCurrentUser().role == 3);
        $scope.logout = function () {
            SystemService.clearSession();
            $state.go('home');
        }
        $scope.user = SystemService.getCurrentUser();
        var promise = StartClubService.getAllNews();
        promise.then(function (data) {
            if (data.code == 0) {
                $scope.allNews = data.returnValue;
                $scope.allNews = $scope.allNews.sort(function (item1, item2) {
                    return item1.news_id > item2.news_id ? -1 : 1;
                });
                //console.log($scope.allNews);
            }
            else
                swal("失败", data.errorReason, "warning", "OK");
        })

        $scope.currentPage = 1;
        $scope.itemsPerPage = 8;
        $scope.maxSize = 5;
        promise = StartClubService.getAllAchievement();
        promise.then(function (data) {
            if (data.code == 0) {
                $scope.allAchievements = data.returnValue;
                $scope.allAchievements = $scope.allAchievements.sort(function (item1, item2) {
                    return item1.achievement_id > item2.achievement_id ? -1 : 1;
                });
                //console.log($scope.allAchievements);
                $scope.totalNum = $scope.allAchievements.length;
                if ($scope.totalNum > $scope.itemsPerPage) {
                    $scope.achievements = $scope.allAchievements.slice(0, $scope.itemsPerPage);
                } else {
                    $scope.achievements = $scope.allAchievements;
                }
            }
            else {
                swal("失败", data.errorReason, "warning", "OK");
            }
        })

        $scope.changePage = function () {
            if ($scope.totalNum > $scope.itemsPerPage * $scope.currentPage)
                $scope.achievements = $scope.allAchievements.slice(($scope.currentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.currentPage);
            else {
                $scope.achievements = $scope.allAchievements.slice(($scope.currentPage - 1) * $scope.itemsPerPage);
            }
        };
        $scope.onSelect = function (item, model, label, event) {
            $scope.keyWords = label;
            var promise = ShowNewsService.setNews(item.news_id);
            promise.then(function (data) {
                $state.go('showNews');
            })
        }
        $scope.goEditSelf = function () {
            ManageUsersService.setCurrentUser(SystemService.getCurrentUser());
            $state.go('editUser');
        }
        $scope.goAddAchievement = function () {
            $state.go('addAchievement');
        }
        $scope.goAddNews = function () {
            $state.go('addNews');
        }
        $scope.goManageUsers = function () {
            $state.go('manageUsers');
        }
        $scope.goToNewsList = function () {
            event.preventDefault();
            var top = $("#newsList").offset().top;
            jQuery("html,body").animate({scrollTop: top}, 500);
        }
        $scope.goClubMembers = function () {
            $state.go('showClubMembers');
        }
        $scope.viewMore = function (news_id) {
            var promise = ShowNewsService.setNews(news_id);
            promise.then(function (data) {
                $state.go('showNews');
            })
        }
        $scope.viewAchievementDetail = function (achievement_id) {
            var promise = ShowAchievementService.setAchievement(achievement_id);
            promise.then(function (data) {
                $state.go('showAchievement');
            })
        }
        $scope.editAchievement = function (achievement_id) {
            var promise = ShowAchievementService.setAchievement(achievement_id);
            promise.then(function (data) {
                $state.go('editAchievement');
            })
        }
        $scope.deleteAchievement = function (achievement_id) {
            var promise = StartClubService.deleteAchievement(achievement_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    var promise = StartClubService.getAllAchievement();
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.allAchievements = data.returnValue;
                            if ($scope.totalNum > $scope.itemsPerPage * $scope.currentPage)
                                $scope.achievements = $scope.allAchievements.slice(($scope.currentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.currentPage);
                            else {
                                $scope.achievements = $scope.allAchievements.slice(($scope.currentPage - 1) * $scope.itemsPerPage);
                            }
                            swal("成功", "删除成功", "success", "OK");
                        }
                        else
                            swal("失败", data.errorReason, "warning", "OK");
                    })
                }
            })
        }
        $scope.deleteNews = function (news_id, index) {
            var promise = StartClubService.deleteNews(news_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    var promise = StartClubService.getAllNews();
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.allNews = data.returnValue;
                            swal("成功", "删除成功", "success", "OK");
                        }
                        else
                            swal("失败", data.errorReason, "warning", "OK");
                    })
                }
            })
        }
        $scope.editNews = function (news_id) {
            var promise = ShowNewsService.setNews(news_id);
            promise.then(function (data) {
                $state.go('editNews');
            })
        }
    })
    //管理员管理用户
    .controller('ManageUsersCtrl', function ($scope, $state, ManageUsersService, SystemService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role != 3) {//只有管理员有权限修改用户
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }
        $scope.AllUsers = [];
        $scope.currentPage = 1;
        $scope.itemsPerPage = 3;
        $scope.maxSize = 5;
        var promise = ManageUsersService.getAllUsers();
        promise.then(function (data) {
            if (data.code == 0) {
                $scope.AllUsers = data.returnValue;
                $scope.totalNum = $scope.AllUsers.length;
                if ($scope.totalNum > $scope.itemsPerPage) {
                    $scope.users = $scope.AllUsers.slice(0, $scope.itemsPerPage);
                } else {
                    $scope.users = $scope.AllUsers;
                }
            }
            else {
                swal("失败", data.errorReason, "warning", "OK");
            }
        });

        $scope.changePage = function () {
            if ($scope.totalNum > $scope.itemsPerPage * $scope.currentPage)
                $scope.users = $scope.AllUsers.slice(($scope.currentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.currentPage);
            else {
                $scope.users = $scope.AllUsers.slice(($scope.currentPage - 1) * $scope.itemsPerPage);
            }
        };
        $scope.edit = function (item) {
            ManageUsersService.setCurrentUser(item);
            $state.go('editUser');
        };
        $scope.back = function () {
            $state.go('startClub');
        }
        $scope.delete = function (user_id, index) {
            swal({
                title: "是否确定?",
                text: "将删除级联该用户所有活动信息且不可恢复",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的",
                cancelButtonText: "取消",
                closeOnConfirm: false,
                closeOnCancel: true
            }, function (isConfirm) {
                if (isConfirm) {
                    var promise = ManageUsersService.deleteUser(user_id);
                    promise.then(function (data) {
                        if (data.code == 0) {
                            swal({
                                title: "成功",
                                text: "删除成功",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定",
                                cancelButtonText: "No, cancel plx!",
                                closeOnConfirm: false,
                                closeOnCancel: false
                            }, function (isConfirm) {
                                location.reload();
                            });
                        } else {
                            swal("失败", data.errorReason, "warning", "OK");
                        }
                    })
                }
            });


        }
        $scope.changeUserState = function (user) {
            var promise = ManageUsersService.changeUserState(user.user_id, !user.user_isuse);
            promise.then(function (data) {
                if (data.code == 0) {
                    user.user_isuse = !user.user_isuse;
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.switchRole = function (user) {
            var promise = ManageUsersService.switchUserRole(user.user_id, (user.role + 1) % 3);
            promise.then(function (data) {
                if (data.code == 0) {
                    user.role = (user.role + 1) % 3;
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        }
    })
    //编辑用户信息
    .controller('EditUserCtrl', function ($scope, $state, $sce, FileUploader, ManageUsersService, SystemService, StartClubService, ShowAchievementService, ShowNewsService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role == 0) {
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }

        $scope.tabIndex = 0;

        $scope.user = ManageUsersService.getCurrentUser();
        $scope.isShow = $scope.user.user_id != Number(window.sessionStorage.user_id);
        $scope.backupUser = {};
        angular.copy($scope.user, $scope.backupUser);//采用深复制
        //点击垂直导航事件
        $scope.achievementCurrentPage = 1;
        $scope.newsCurrentPage = 1;
        $scope.itemsPerPage = 4;
        $scope.maxSize = 5;
        $scope.switchTabTo0 = function () {
            event.preventDefault();//取消点击的默认动作
            $scope.tabIndex = 0;
        };
        $scope.switchTabTo1 = function () {
            event.preventDefault();//取消点击的默认动作
            $scope.tabIndex = 1;
        };
        $scope.switchTabTo2 = function () {
            event.preventDefault();//取消点击的默认动作
            $scope.tabIndex = 2;
        };
        $scope.switchTabTo3 = function () {
            event.preventDefault();//取消点击的默认动作
            $scope.tabIndex = 3;

        }
        $scope.switchTabTo4 = function () {
            event.preventDefault();//取消点击的默认动作
            $scope.tabIndex = 4;
        }


        var promise = StartClubService.getAllMyAchievement(SystemService.getCurrentUser().user_id);
        promise.then(function (data) {
            if (data.code == 0) {
                $scope.allAchievements = data.returnValue;
                $scope.achievementTotalNum = $scope.allAchievements.length
                $scope.allAchievements = $scope.allAchievements.sort(function (item1, item2) {
                    return item1.achievement_id > item2.achievement_id ? -1 : 1;
                });
                //console.log($scope.allAchievements);
                if ($scope.achievementTotalNum > $scope.itemsPerPage) {
                    $scope.achievements = $scope.allAchievements.slice(0, $scope.itemsPerPage);
                } else {
                    $scope.achievements = $scope.allAchievements;
                }
            }
            else {
                swal("失败", data.errorReason, "warning", "OK");
            }
        })
        if ($scope.user.role == 3) {
            var promise = StartClubService.getAllMyNews(SystemService.getCurrentUser().user_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.allNews = data.returnValue;
                    $scope.newsTotalNum = $scope.allNews.length
                    $scope.allNews = $scope.allNews.sort(function (item1, item2) {
                        return item1.news_id > item2.news_id ? -1 : 1;
                    });
                    //console.log($scope.allNews);
                    if ($scope.newsTotalNum > $scope.itemsPerPage) {
                        $scope.newses = $scope.allNews.slice(0, $scope.itemsPerPage);
                    } else {
                        $scope.newses = $scope.allNews;
                    }
                }
                else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        }

        $scope.achievementChangePage = function () {
            if ($scope.achievementTotalNum > $scope.itemsPerPage * $scope.achievementCurrentPage)
                $scope.achievements = $scope.allAchievements.slice(($scope.achievementCurrentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.achievementCurrentPage);
            else {
                $scope.achievements = $scope.allAchievements.slice(($scope.achievementCurrentPage - 1) * $scope.itemsPerPage);
            }
        };
        $scope.newsChangePage = function () {
            if ($scope.newsTotalNum > $scope.itemsPerPage * $scope.newsCurrentPage)
                $scope.newses = $scope.allNews.slice(($scope.newsCurrentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.newsCurrentPage);
            else {
                $scope.newses = $scope.allNews.slice(($scope.newsCurrentPage - 1) * $scope.itemsPerPage);
            }
        }
        $scope.editNews = function (news_id) {
            var promise = ShowNewsService.setNews(news_id);
            promise.then(function (data) {
                $state.go('editNews');
            })
        }

        $scope.deleteNews = function (news_id) {
            var promise = StartClubService.deleteNews(news_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    var promise = StartClubService.getAllNews();
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.allNews = data.returnValue;
                            if ($scope.newsTotalNum > $scope.itemsPerPage * $scope.newsCurrentPage)
                                $scope.newses = $scope.allNews.slice(($scope.newsCurrentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.newsCurrentPage);
                            else {
                                $scope.newses = $scope.allNews.slice(($scope.newsCurrentPage - 1) * $scope.itemsPerPage);
                            }
                            swal("成功", "删除成功", "success", "OK");
                        }
                        else
                            swal("失败", data.errorReason, "warning", "OK");
                    })
                }
            })
        }

        $scope.editAchievement = function (achievement_id) {
            var promise = ShowAchievementService.setAchievement(achievement_id);
            promise.then(function (data) {
                $state.go('editAchievement');
            })
        }

        $scope.deleteAchievement = function (achievement_id) {
            var promise = StartClubService.deleteAchievement(achievement_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    var promise = StartClubService.getAllAchievement();
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.allAchievements = data.returnValue;
                            if ($scope.achievementTotalNum > $scope.itemsPerPage * $scope.achievementCurrentPage)
                                $scope.achievements = $scope.allAchievements.slice(($scope.achievementCurrentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.achievementCurrentPage);
                            else {
                                $scope.achievements = $scope.allAchievements.slice(($scope.achievementCurrentPage - 1) * $scope.itemsPerPage);
                            }
                            swal("成功", "删除成功", "success", "OK");
                        }
                        else
                            swal("失败", data.errorReason, "warning", "OK");
                    })
                }
            })
        }


        $scope.back = function () {
            event.preventDefault();//取消点击的默认动作
            if ($scope.isShow)
                $state.go('manageUsers');
            else
                $state.go('startClub');
        };
        $scope.reset = function () {
            angular.copy($scope.backupUser, $scope.user);//采用深复制
        };
        //重置密码
        $scope.newpwd = '';
        $scope.repeatpwd = '';
        $scope.htmlPopover = $sce.trustAsHtml('<b style="color: red">两次密码不一致</b>');
        $scope.checkPwd = function () {
            if ($scope.newpwd != $scope.repeatpwd) {
                $scope.isOpen = true;
            } else {
                $scope.isOpen = false;
            }
        };
        $scope.checkPhoneLength = function () {
            if (!/^\d{11}$/.test($scope.user.mobile))
                $scope.mobileError = 1;
            else {
                $scope.mobileError = 0;
            }
        }
        $scope.checkEmail = function () {
            if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test($scope.user.email)) {
                $scope.emailError = 1;
            } else {
                $scope.emailError = 0;
            }
        }

        $scope.checkPassLength = function () {
            if ($scope.newpwd.length < 6)
                $scope.passwordError = 1;
            else {
                $scope.passwordError = 0;
            }
        }
        $scope.updatePwd = function () {
            $scope.checkPassLength();
            $scope.checkPwd();
            if ($scope.isOpen || $scope.passwordError) {
                return;
            }
            var promise = ManageUsersService.updatePwd($scope.user.user_id, $scope.newpwd);
            promise.then(function (data) {
                if (data.code == 0) {
                    swal("成功", data.returnValue, "success", "OK");
                    $scope.newpwd = "";
                    $scope.repeatpwd = "";
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.updateUser = function () {
            $scope.checkPhoneLength();
            $scope.checkEmail();
            if ($scope.emailError || $scope.mobileError) {
                return;
            }
            for (var key in $scope.user) {
                if (!$scope.user[key]) {
                    if (key != "role" && key != "user_isuse") {
                        swal("失败", "请完全填写表单!", "warning", "OK");
                        return;
                    }
                }
            }
            var promise = ManageUsersService.updateUser($scope.user);
            promise.then(function (data) {
                if (data.code == 0) {
                    swal("成功", data.returnValue, "success", "OK");
                    window.sessionStorage.nickname = $scope.user.nickname;
                    window.sessionStorage.email = $scope.user.email;
                    window.sessionStorage.mobile = $scope.user.mobile;
                    window.sessionStorage.realname = $scope.user.realname;
                }
                else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        //并未解决怎么同时传图片和参数的问题
        var uploader = $scope.uploader = new FileUploader({
            url: SystemService.getHostIp() + "upload",
        });

        uploader.onCompleteItem = function (fileItem, response, status, headers) {
            if (response.code == 0) {
                $scope.user.headimage = response.returnValue;
                var promise = ManageUsersService.updateHeadImage($scope.user.user_id, response.returnValue);
                promise.then(function (data) {
                    //console.log(data);
                    if (data.code == 0) {
                        swal("成功", "上传成功", "success", "OK");
                        window.sessionStorage.headimage = data.returnValue;
                        $scope.user.headimage = data.returnValue;
                        document.getElementById("submitImg").reset();
                        //fileItem.remove();
                    } else {
                        swal("失败", data.errorReason, "warning", "OK");
                    }
                })
            } else {
                swal("失败", data.errorReason, "warning", "OK");
            }
        };
    })
    .controller('AddNewsCtrl', function ($scope, $state, SystemService, AddNewsService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role != 3) {
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }
        $scope.newsContent = "";
        $scope.newsTitle = "";
        $scope.isClick = false;
        $scope.publish = function () {
            if (!$scope.isClick) {
                $scope.isClick = true;
                var promise = AddNewsService.publish($scope.newsTitle, $scope.newsContent, SystemService.getCurrentUser().user_id);
                //var promise = AddNewsService.publish($scope.newsTitle, $scope.newsContent, 1);
                promise.then(function (data) {
                    if (data.code == 0) {
                        swal("成功", "发表成功", "success", "OK");
                        $state.go('startClub');
                    } else {
                        swal("失败", data.errorReason, "warning", "OK");
                    }
                    $scope.isClick = false;
                })
            }
        }
        $scope.back = function () {
            $state.go('startClub');
        }
    })
    .controller('ShowNewsCtrl', function ($scope, ShowNewsService, $state, SystemService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        }
        $scope.user = SystemService.getCurrentUser();
        $scope.news = ShowNewsService.getNews();
        $("#news_content").html($scope.news.news_content);
        $scope.nextNews = function () {
            var promise = ShowNewsService.nextNews($scope.news.news_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.news = data.returnValue;
                    $("#news_content").html($scope.news.news_content);
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.preNews = function () {
            var promise = ShowNewsService.preNews($scope.news.news_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.news = data.returnValue;
                    $("#news_content").html($scope.news.news_content);
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.back = function () {
            $state.go('startClub', {}, {reload: true});
        }
        //返回顶部
        //$scope.goToTop = function(){
        //    event.preventDefault();
        //    var top = $("#top").offset().top;
        //    jQuery("html,body").animate({scrollTop:top},300);
        //}
        $scope.publishComment = function () {
            var comment = $('#Smohan_text').val();
            $('#Smohan_text').val("");
            for (i = 0; i < 60; i++) {
                comment = comment.replace('<emt>' + (i + 1) + '</emt>', '<img src="images/face/' + (i + 1) + '.gif">');
            }
            var promise = ShowNewsService.addComment(comment);
            promise.then(function (data) {
                if (data.code == 0) {
                    promise = ShowNewsService.setNews($scope.news.news_id);
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.news = data.returnValue;
                            swal("成功", "评论成功", "success", "OK");
                        }
                    })
                }
            })
        }
        $scope.deleteComment = function (comment_id, index) {
            swal({
                    title: "是否确定?",
                    text: "确定删除此条评论!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes!",
                    closeOnConfirm: false
                },
                function () {
                    var promise = ShowNewsService.deleteComment(comment_id);
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.news.comments.splice(index, 1);
                            swal("成功", "删除成功", "success", "OK");
                        } else {
                            swal("失败", data.errorReason, "warning", "OK");
                        }
                    })
                });

        }
    })
    .controller('EditNewsCtrl', function ($scope, $state, ShowNewsService, EditNewsService, SystemService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role != 3) {
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }
        $scope.news = ShowNewsService.getNews();

        $scope.update = function () {
            var promise = EditNewsService.update($scope.news.news_title, $scope.news.news_content, $scope.news.news_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    swal("成功", "修改成功", "success", "OK");
                    $state.go('startClub');
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        }
        $scope.back = function () {
            $state.go('startClub');
        }
    })
    .controller('AddAchievementCtrl', function ($scope, $state, SystemService, AddAchievementService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role == 0 || SystemService.getCurrentUser().role == 1) {//游客和非社团成员没有发表成果的权限
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }
        $scope.achievementContent = "";
        $scope.achievementTitle = "";
        $scope.isClick = false;
        $scope.publish = function () {
            if (!$scope.isClick) {
                $scope.isClick = true;
                var promise = AddAchievementService.publish($scope.achievementTitle, $scope.achievementContent, SystemService.getCurrentUser().user_id);
                //var promise = AddAchievementService.publish($scope.achievementTitle, $scope.achievementContent, 1);
                promise.then(function (data) {
                    if (data.code == 0) {
                        swal("成功", "发表成功", "success", "OK");
                        $state.go('startClub');
                    } else {
                        swal("失败", data.errorReason, "warning", "OK");
                    }
                    $scope.isClick = false;
                })
            }
        }
        $scope.back = function () {
            $state.go('startClub');
        }
    })
    .controller('EditAchievementCtrl', function ($scope, $state, ShowAchievementService, EditAchievementService, SystemService) {
        if (!window.sessionStorage.user_id) {
            swal("抱歉", "请您登陆", "warning", "OK");
            $state.go('home');
        } else if (SystemService.getCurrentUser().role == 0 || SystemService.getCurrentUser().role == 1) {//游客和非社团成员没有发表成果的权限
            swal("抱歉", "您没有该权限", "warning", "OK");
            $state.go('startClub');
        }

        $scope.achievement = ShowAchievementService.getAchievement();

        $scope.update = function () {
            var promise = EditAchievementService.update($scope.achievement.achievement_title, $scope.achievement.achievement_content, $scope.achievement.achievement_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    swal("成功", "修改成功", "success", "OK");
                    if (SystemService.getCurrentUser().role != 3)
                        $state.go('editUser');
                    else
                        $state.go('startClub');
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        }
        $scope.back = function () {
            if (SystemService.getCurrentUser().role != 3)
                $state.go('editUser');
            else
                $state.go('startClub');
        }
    })
    .controller('ShowAchievementCtrl', function ($scope, ShowAchievementService, $state, SystemService) {
        if (!window.sessionStorage.user_id) {
            swal("失败", "请您登陆", "warning", "OK");
            $state.go('home');
        }
        $scope.user = SystemService.getCurrentUser();
        $scope.achievement = ShowAchievementService.getAchievement();
        $("#achievement_content").html($scope.achievement.achievement_content);
        $scope.nextAchievement = function () {
            var promise = ShowAchievementService.nextAchievement($scope.achievement.achievement_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.achievement = data.returnValue;
                    $("#achievement_content").html($scope.achievement.achievement_content);
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.preAchievement = function () {
            var promise = ShowAchievementService.preAchievement($scope.achievement.achievement_id);
            promise.then(function (data) {
                if (data.code == 0) {
                    $scope.achievement = data.returnValue;
                    $("#achievement_content").html($scope.achievement.achievement_content);
                } else {
                    swal("失败", data.errorReason, "warning", "OK");
                }
            })
        };
        $scope.back = function () {
            $state.go('startClub', {}, {reload: true});
        }
        //返回顶部
        //$scope.goToTop = function(){
        //    event.preventDefault();
        //    var top = $("#top").offset().top;
        //    jQuery("html,body").animate({scrollTop:top},300);
        //}
        $scope.publishComment = function () {
            var comment = $('#Smohan_text').val();
            $('#Smohan_text').val("");
            for (i = 0; i < 60; i++) {
                comment = comment.replace('<emt>' + (i + 1) + '</emt>', '<img src="images/face/' + (i + 1) + '.gif">');
            }
            var promise = ShowAchievementService.addComment(comment);
            promise.then(function (data) {
                if (data.code == 0) {
                    promise = ShowAchievementService.setAchievement($scope.achievement.achievement_id);
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.achievement = data.returnValue;
                            swal("成功", "评论成功", "success", "OK");
                        }
                    })
                }
            })
        }
        $scope.deleteComment = function (comment_id, index) {
            swal({
                    title: "是否确定?",
                    text: "确定删除此条评论!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes!",
                    closeOnConfirm: false
                },
                function () {
                    var promise = ShowAchievementService.deleteComment(comment_id);
                    promise.then(function (data) {
                        if (data.code == 0) {
                            $scope.achievement.comments.splice(index, 1);
                            swal("成功", "删除成功", "success", "OK");
                        } else {
                            swal("失败", data.errorReason, "warning", "OK");
                        }
                    })
                });
        }
    })
    .controller('ShowClubMembersCtrl', function ($scope, $state, ShowClubMembersService) {
        $scope.currentPage = 1;
        $scope.itemsPerPage = 6;
        $scope.maxSize = 5;
        var promise = ShowClubMembersService.getAllClubMembers();
        promise.then(function (data) {
            if (data.code == 0) {
                $scope.members = data.returnValue;
                $scope.totalNum = $scope.members.length;
                if ($scope.totalNum > $scope.itemsPerPage * $scope.currentPage)
                    $scope.someMembers = $scope.members.slice(($scope.currentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.currentPage);
                else {
                    $scope.someMembers = $scope.members.slice(($scope.currentPage - 1) * $scope.itemsPerPage);
                }
            } else {
                swal("失败", data.errorReason, "warning", "OK");
            }
        })
        $scope.changePage = function () {
            if ($scope.totalNum > $scope.itemsPerPage * $scope.currentPage)
                $scope.someMembers = $scope.members.slice(($scope.currentPage - 1) * $scope.itemsPerPage, $scope.itemsPerPage * $scope.currentPage);
            else {
                $scope.someMembers = $scope.members.slice(($scope.currentPage - 1) * $scope.itemsPerPage);
            }
        };
        $scope.back = function () {
            $state.go('startClub');
        }
    })